/**********************************************
** @Author: phy
** @Date:   2017-12-12 14:17:37
** @Last Modified by:   phy
***********************************************/

package models

import (
	"fmt"
	"time"

	"WebAdminSys/libs"
	"github.com/astaxie/beego/orm"
)


func TimerTasks(iSecond int) {
	return;
	RoomBloodChangeDataticker := time.NewTicker(time.Second * 120)
	//ticker1 := time.NewTicker(time.Second * 1)

	//定时采集用户在线数据 插入AllUserOnline 表
	AllUserOnlineCollectTicker := time.NewTicker(time.Second * 120);
	//定时采集用户每天登录注册
	UpDateDayPlathFormInfoTicker := time.NewTicker(time.Second * 5);
	//12点更新
	
	
	now := time.Now()
    // 计算下一个零点
    next := now.Add(time.Hour * 24)
    next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location())
    UpDateZeroTimerTick :=  time.NewTimer(next.Sub(now))

	go func() {
	   for {
	  			select {
					  	case <-RoomBloodChangeDataticker.C:
					   	{
					   		RoomBloodChangeData();
					   	}
					   	//case <-ticker1.C:
					   //	{
					   		//testTimer2();
					   	//}
						case <- AllUserOnlineCollectTicker.C:
						{
							AllUserOnlineCountInsertValue();
						}
						case <- UpDateDayPlathFormInfoTicker.C:
						{
							UpDateDayPlathFormOnLineInfo();
						}
						case <- UpDateZeroTimerTick.C:
						{
							UpDateDayPlathFormInfo();
							now := time.Now()
						    // 计算下一个零点
						    next := now.Add(time.Hour * 24)
						    next = time.Date(next.Year(), next.Month(), next.Day(), 0, 0, 0, 0, next.Location())
						    UpDateZeroTimerTick =  time.NewTimer(next.Sub(now))
						}
	  			}
 			}
	}()
}

func RoomBloodChangeData(){
	go func() {
	 	xMutex := libs.GetxMutex();
	 	xMutex.XMutexLock();
		result, count := RoomCheatMainGetAllList();
		list := make([]RoomBloodChange, len(result));
		for k,v := range result {
			list[k].ServerID = v.RoomID;
			list[k].WriteDateTime = libs.GetCurSysTmeTimeToUnix();
			list[k].RoomBloodBalance = v.RoomBloodBalance;
			list[k].BloodScore = v.RoomBloodPool;
		}
		iCount := int(count)
		orm.NewOrm().InsertMulti(iCount,list);
		defer xMutex.XMutexUnLock();
	}()
}

func testTimer2() {
	return;
	 go func() {
	 		iWriteDateTime := libs.GetCurSysTmeTimeToUnix();
	 		//iRoomBloodBalance := 10000
	 		for i := 0; i < 100;i++{	
	  
	  			iWriteDateTime += 20
	  			iBloodScore := libs.RandInt64(-20000,20000)
	  			data := RoomBloodChange{
	  				ServerID:501,
	  				WriteDateTime:iWriteDateTime,
	  				RoomBloodBalance:10000,
	  				BloodScore:iBloodScore};

	  			orm.NewOrm().Insert(&data);
	  		}
	 	//fmt.Printf("随机数测试:%d",)

	 }()
}
func UpDateDayPlathFormInfo(){
	go func() {
	 	xMutex := libs.GetxMutex();
	 	xMutex.XMutexLock();

		TodayZeroTime := libs.GetYesterdayUnixZeroTime();
		TodayNoTimeS := TodayZeroTime + 86399;;
		i64RegCount := ExtremVipGetRegCount(TodayZeroTime,TodayNoTimeS);
		i64LogonCount := ExtremVipGetLoginLobbyCount(TodayZeroTime,TodayNoTimeS);
		i64RMB := ExtremVipGetUserCharge(TodayZeroTime,TodayNoTimeS);
		iTopOnLine := ExtremVipGetUserOnline();
		i64GMAddGold := ExtremGMAddGoldS(TodayZeroTime,TodayNoTimeS);
		i64SysGold := ExtremGetSysGold();
		i64UserGold := ExtremGetUserGold();
		i64VIPGold := ExtremGetVIPUserGold();
		i64SellScore, i64SellCount,i64BuyScore,i64BuyCount,i64UserCount := ExtremVipSendGitfListNum(TodayZeroTime,TodayNoTimeS);
		i64SysWinLose := ExtremSysWinLoseData(TodayZeroTime)
		i64BloodScore := ExtremVipGameBooldData();
		o := orm.NewOrm()
		o.Using("default")
		Gettable := o.QueryTable(TableName("DayPlathFormInfo"));
		exist := Gettable.Filter("DataDateTime", TodayZeroTime).Exist()
		if !exist {
			DayDataInfo := DayPlathFormInfo{
		 		DataDateTime:TodayZeroTime,
		 		RegCount:i64RegCount,
		  		LogonCount:i64LogonCount,
		  		RMB:i64RMB,	
		  		TopOnLine:iTopOnLine,
		  		GMAddGold:i64GMAddGold,
		  		SysGold :i64SysGold,
		  		UserGold:i64UserGold,
		  		VIPGold:i64VIPGold,
		  		SellScore:i64SellScore,
		  		SellCount:i64SellCount, 
		  		SysWinLose:i64SysWinLose,
		  		BloodScore:i64BloodScore,
		  		BuyScore:i64BuyScore,
		  		BuyCount:i64BuyCount,
		  		UserCount:i64UserCount,
			}
			inSt, _ := Gettable.PrepareInsert()
			inSt.Insert(&DayDataInfo);
			defer inSt.Close() // 别忘记关闭 statement
		}else{
			DayDataInfo := DayPlathFormInfo{
		 		DataDateTime:TodayZeroTime,
		 	}
		 	
		 	if o.Read(&DayDataInfo,"DataDateTime") == nil {
		 		fmt.Println("kkkkkkkkkkkkk",DayDataInfo)
		 		DayDataInfo.RegCount = i64RegCount;
		 		DayDataInfo.LogonCount = i64LogonCount;
		 		DayDataInfo.RMB = i64RMB;
		 		if iTopOnLine > DayDataInfo.TopOnLine {
		 			DayDataInfo.TopOnLine = iTopOnLine;
		 		}
		 		DayDataInfo.GMAddGold = i64GMAddGold;
		 		DayDataInfo.SysGold = i64SysGold;
		 		DayDataInfo.UserGold = i64UserGold;
		 		DayDataInfo.VIPGold =  i64VIPGold;
		 		DayDataInfo.SellScore = i64SellScore;
		  		DayDataInfo.SellCount = i64SellCount; 
		  		DayDataInfo.SysWinLose = i64SysWinLose;
		  		DayDataInfo.BloodScore = i64BloodScore;
		  		DayDataInfo.BuyScore = i64BuyScore;
		  		DayDataInfo.BuyCount = i64BuyCount;
		  		DayDataInfo.UserCount = i64UserCount;
		 		o.Update(&DayDataInfo)
		 	}
		}
		
		defer xMutex.XMutexUnLock();
	}()
}


func UpDateDayPlathFormOnLineInfo(){
	go func() {
	 	xMutex := libs.GetxMutex();
	 	xMutex.XMutexLock();

		TodayZeroTime := libs.GetUnixZeroTime();
		iTopOnLine := ExtremVipGetUserOnline();
		
		o := orm.NewOrm()
		o.Using("default")
		Gettable := o.QueryTable(TableName("DayPlathFormInfo"));
		exist := Gettable.Filter("DataDateTime", TodayZeroTime).Exist()
		if !exist {
			DayDataInfo := DayPlathFormInfo{
		 		DataDateTime:TodayZeroTime,
		  		TopOnLine:iTopOnLine,
			}
			inSt, _ := Gettable.PrepareInsert()
			inSt.Insert(&DayDataInfo);
			defer inSt.Close() // 别忘记关闭 statement
		}else{
			DayDataInfo := DayPlathFormInfo{
		 		DataDateTime:TodayZeroTime,
		 	}
		 	
		 	if o.Read(&DayDataInfo,"DataDateTime") == nil {
		 		fmt.Println("kkkkkkkkkkkkk",DayDataInfo)
		 		if iTopOnLine > DayDataInfo.TopOnLine {
		 			DayDataInfo.TopOnLine = iTopOnLine;
		 		}
		 	
		 		o.Update(&DayDataInfo,"TopOnLine")
		 	}
		}
		
		defer xMutex.XMutexUnLock();
	}()
}
      